/**
 * ReportGear(2011)
 */
package com.reportgear.designer.editor.object.pane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.designer.editor.object.Editor;
import com.reportgear.designer.editor.object.TextEditor;
import com.reportgear.designer.gui.component.button.CustomTextButton;

/**
 * 通用对象编辑器面板
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ValueEditorPane extends JBasePane {
	private Editor[] cards;
	private Editor currentEditor;
	private CustomTextButton arrowButton;
	private JPopupMenu menu;
	private JPanel centerPane;

	public ValueEditorPane(Editor[] editorArray) {
		this(editorArray, null, null);
	}

	public ValueEditorPane(Editor[] editorArray, String paramString1, String paramString2) {
		initComponents(editorArray, paramString1, paramString2);
	}

	private void initComponents(Editor[] editorArray, String paramString1, String paramString2) {
		this.cards = editorArray;
		this.setLayout(new BorderLayout());
		this.centerPane = new JPanel(new BorderLayout());

		this.arrowButton = new CustomTextButton("String");
		setCurrentEditor(0);
		this.centerPane.setPreferredSize(new Dimension(200, this.centerPane.getPreferredSize().height));
		// this.arrowButton.setUI(new MotifButtonUI());
		final Color localColor = this.arrowButton.getBackground();
		this.menu = createPopMenu();
		this.arrowButton.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent paramMouseEvent) {
				arrowButton.setBackground(Color.WHITE);
				arrowButton.repaint();
			}

			public void mouseExited(MouseEvent paramMouseEvent) {
				arrowButton.setBackground(localColor);
			}
		});
		this.arrowButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent paramActionEvent) {
				Rectangle localRectangle = ValueEditorPane.this.centerPane.getBounds();
				ValueEditorPane.this.menu.setPopupSize(localRectangle.width
						+ ValueEditorPane.this.arrowButton.getWidth(),
						ValueEditorPane.this.menu.getPreferredSize().height);
				ValueEditorPane.this.menu.show(ValueEditorPane.this.centerPane, -ValueEditorPane.this.arrowButton
						.getWidth(), localRectangle.height);
			}
		});
		add(this.centerPane, BorderLayout.CENTER);
		add(this.arrowButton, BorderLayout.WEST);
	}

	private void setCurrentEditor(int index) {
		this.currentEditor = this.cards[index];

		addStringListener();
		this.centerPane.removeAll();
		this.centerPane.add(this.currentEditor);
		this.centerPane.validate();
		this.centerPane.repaint();

		this.arrowButton.setText(this.cards[index].getName());
	}

	private JPopupMenu createPopMenu() {
		JPopupMenu popupMenu = new JPopupMenu();
		if (this.cards == null) {
			return popupMenu;
		}

		for (int i = 0; i < this.cards.length; ++i) {
			JMenuItem menuItem = new JMenuItem(this.cards[i].getName());
			final int j = i;
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent act) {
					ValueEditorPane.this.setCurrentEditor(j);
					ValueEditorPane.this.popFormulaEditor();
				}
			});
			popupMenu.add(menuItem);

			if (i >= this.cards.length - 1) {
				continue;
			}

			popupMenu.addSeparator();
		}

		return popupMenu;
	}

	public void populate(Object value) {
		for (int i = 0; i < this.cards.length; ++i) {
			if (!(this.cards[i].accept(value))){
				continue;
			}
			
			setCurrentEditor(i);
			this.currentEditor.setValue(value);
			
			return;
		}
	}

	public Object update() {
		return this.currentEditor.getValue();
	}

	private void popFormulaEditor() {
		System.out.println(this.currentEditor.getName());
		if (!(this.currentEditor.getName().equals("公式参数"))) {
			return;
		}
		// FormulaPane localFormulaPane = new FormulaPane();
		// // localFormulaPane.populate(new Formula(""));
		// localFormulaPane.showWindow(SwingUtilities.getWindowAncestor(this),
		// new DialogActionListener(localFormulaPane) {
		// public void doOk() {
		// ValueEditorPane.this.currentEditor.setValue(this.val$formulaPane.update());
		// }
		// }).setVisible(true);
	}

	public void setEditors(Editor[] editorArray, Object value) {
		this.cards = editorArray;
		populate(value);
	}

	private void addStringListener() {
		if (!(this.currentEditor instanceof TextEditor)) {
			return;
		}
		// int j = 0;
		// for (int i = 0; i < this.cards.length; ++i) {
		// if (!(this.cards[i].getName().equals("公式参数")))
		// continue;
		// j = 1;
		// break;
		// }
		// if (j == 0)
		// return;
		// int k = i;
		// ((TextEditor) this.currentEditor).getTextField().addFocusListener(new
		// FocusAdapter() {
		// public void focusLost(FocusEvent paramFocusEvent) {
		// if (!(ValueEditorPane.this.currentEditor instanceof TextEditor))
		// return;
		// String str = (String) ValueEditorPane.this.currentEditor.getValue();
		// if ((!(StringUtils.isEmpty(str))) || (!(str.startsWith("="))))
		// return;
		//		
		// if (!(localDesignerEnvManager.isSupportStringToFormula()))
		// return;
		// if (!(localDesignerEnvManager.isDefaultStringToFormula())) {
		// int i =
		//		 
		// JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(ValueEditorPane.this),
		// "公式编辑", "确定", 0);
		// if (i == 0) {
		// ValueEditorPane.this.setCurrentEditor(this.val$j);
		// Formula localFormula2 = new Formula(str);
		//		  
		// ValueEditorPane.this.currentEditor.setValue(localFormula2);
		// }
		// } else {
		// ValueEditorPane.this.setCurrentEditor(this.val$j);
		// Formula localFormula1 = new Formula(str);
		//
		// ValueEditorPane.this.currentEditor.setValue(localFormula1);
		// }
		// }
		// });
	}
}